home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) Silicon Graphics, Inc. 1996 */
-
- /* earth.c
- * This program maps a texture of the earth onto a sphere.
- * It uses a quadric object to create the sphere and must
- * tell the quadric to generate texture coordinates.
- *
- * The texture environment mode can be changed interactively.
- *
- * <e> Key - cycle through environment modes
- * <s> Key - toggle earth rotation on/off
- * <t> Key - toggle texturing on/off
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initTexture( unsigned int *, GLsizei, GLsizei );
-
- void resetView( GLvoid );
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat spin = 0.0;
- static GLuint envmode = 0;
-
- static GLUquadricObj *quadObj;
-
- typedef struct {
- GLint type;
- char *name;
- } EnvModeInfo;
-
- static EnvModeInfo modes[4] = {
- { GL_DECAL, "GL_DECAL" },
- { GL_MODULATE, "GL_MODULATE" },
- { GL_BLEND, "GL_BLEND" },
- { GL_REPLACE_EXT, "GL_REPLACE_EXT" },
- };
-
- static GLboolean spinFlag = GL_TRUE;
- static GLboolean textureFlag = GL_TRUE;
-
- void
- main ( int argc, char *argv[])
- {
- GLsizei width, height;
- char *imageFileName = "earth.rgb";
- unsigned int *image;
- GLsizei imageWidth, imageHeight;
-
- glutInit( &argc, argv );
-
- image = rgbReadImageFile(imageFileName, &imageWidth,
- &imageHeight);
-
- if ( image == NULL ) {
- fprintf( stderr, "%s: Error reading %s\n", argv[0], imageFileName );
- exit(-1);
- }
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initTexture( image, imageWidth, imageHeight );
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates texture environment modes\n\n"
- "<e> Key - cycle through texture environment modes\n"
- "<s> Key - toggle earth rotation on/off\n"
- "<t> Key - toggle texturing on/off\n"
- "Escape Key - exit the program\n\n",
- progname);
- printf("\nTexture Environment Mode is %s\n", modes[envmode].name);
- }
-
- GLvoid
- initgfx()
- {
- /* infinite light shining from +x to origin */
- GLfloat light_pos[] = { 1.0, 0.0, 0.0, 0.0 };
- GLfloat mat[] = { 1.0, 1.0, 1.0, 1.0 };
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
-
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
- glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
- glEnable( GL_LIGHT0 );
- glEnable( GL_LIGHTING );
-
- glEnable( GL_DEPTH_TEST );
-
- quadObj = gluNewQuadric ();
- gluQuadricTexture( quadObj, GL_TRUE );
- gluQuadricDrawStyle( quadObj, GLU_FILL );
- gluQuadricNormals( quadObj, GLU_SMOOTH );
- }
-
- GLvoid
- initTexture( unsigned int *image,
- GLsizei imageWidth, GLsizei imageHeight )
- {
- GLfloat whitecol[] = { 1.0, 1.0, 1.0, 1.0 };
-
- /* use gluBuild2DMipmaps to create and load mipmaps (it will
- * also scale he original image to be a power of two, if
- * necessary)
- *
- * gluBuild2DMipmaps( target, components, width, height,
- * format, type, imageArray )
- */
-
- gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
- GL_RGBA, GL_UNSIGNED_BYTE, image );
-
- /* Setting the magnification filter to nearest instead of linear
- * may run faster on some platforms, with possibly lower quality
- */
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
-
- /* Used when tex env mode is GL_BLEND */
- glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, whitecol );
-
- /* enable 2D texture mapping */
- glEnable( GL_TEXTURE_2D );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 1.0, 50.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -12.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- spin = fmodf( spin + 1.0, 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE && spinFlag) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- cycleTexEnvMode( GLvoid )
- {
- envmode = (envmode + 1) % 4;
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
- printf("Texture Environment Mode is %s\n", modes[envmode].name );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'e': /* cycle through texture environment modes */
- cycleTexEnvMode();
- glutPostRedisplay();
- break;
- case 's': /* toggle rotation */
- spinFlag = !spinFlag;
- if (spinFlag)
- glutIdleFunc( animate );
- else
- glutIdleFunc( NULL);
- break;
- case 't': /* toggle rotation */
- textureFlag = !textureFlag;
- if (textureFlag)
- glEnable( GL_TEXTURE_2D );
- else
- glDisable( GL_TEXTURE_2D );
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- drawScene(void)
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- glRotatef( spin, 0.0, 1.0, 0.0 );
- glRotatef (270.0, 1.0, 0.0, 0.0);
- gluSphere(quadObj, 1.0, 16, 16);
- glPopMatrix();
-
- glutSwapBuffers();
- }
-